<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button-group class="filter-item">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >
          新增
        </el-button>
      </el-button-group>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      keep-alive
      height="100%"
      class="table-container"
      highlight-current-row
    >
      <el-table-column
        fixed
        label="ID"
        width="80"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column
        label="角色名称"
        align="center"
        width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.display_name }}
        </template>
      </el-table-column>
      <el-table-column
        label="角色标识"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="400"
        align="center"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              v-if="scope.row.id != 1"
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope)"
            >
              修改
            </el-button>
            <router-link :to="'/system/group?role_id='+scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit">
                成员管理
              </el-button>
            </router-link>
            <el-button
              v-if="scope.row.id != 1"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope)"
            >
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'modify' ? '修改' : '新增'"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-width="120px"
        label-position="right"
      >
        <el-form-item label="角色名称">
          <el-input v-model="temp.display_name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="角色标识">
          <el-input v-model="temp.name" placeholder="请输入角色标识" />
        </el-form-item>
        <el-form-item v-if="dialogType === 'modify'" label="操作权限">
          <el-tree
            ref="tree"
            :data="roles"
            :props="defaultRolesProps"
            show-checkbox
            accordion
            node-key="id"
            class="permission-tree"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="temp.description" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div class="text-right">
        <el-button type="danger" @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, save_permissions, del } from '@/api/role'
import { getList as getRoles } from '@/api/permission'
import { deepClone } from '@/utils'

const _temp = {
  parent_id: 0,
  display_name: '',
  name: '',
  permission: [],
  description: ''
}

export default {
  components: {
    Pagination
  },
  data() {
    return {
      total: 0,
      list: [],
      roles: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        keyword: undefined
      },
      index: 0,
      temp: Object.assign({}, _temp),
      dialogVisible: false,
      dialogType: 'create',
      loading: false,
      defaultRolesProps: {
        children: 'children',
        label: 'display_name'
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchMenu()
  },
  methods: {
    search() {
      this.fetchData()
    },
    refresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        keyword: undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    fetchMenu() {
      getRoles().then(response => {
        this.roles = response.data.list
      })
    },
    resetTemp() {
      this.temp = Object.assign({}, _temp)
    },
    add() {
      this.resetTemp()
      this.index = ''
      this.dialogVisible = true
      this.dialogType = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    edit(scope) {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogType = 'modify'
      this.index = scope.$index
      this.temp = deepClone(scope.row)
      this.$nextTick(() => {
        this.$refs.tree.setCheckedKeys(scope.row.permission)
        this.$refs['dataForm'].clearValidate()
      })
    },
    del(scope) {
      this.$confirm('确认删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const obj = {
          id: scope.row.id
        }
        del(obj).then(response => {
          setTimeout(() => {
            this.list.splice(scope.$index, 1)
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          }, 300)
        })
      })
    },
    submit() {
      if (this.loading) {
        return
      }
      this.loading = true
      const obj = this.temp
      if (this.dialogType === 'modify') {
        obj['permissions_id'] = this.$refs.tree.getCheckedKeys()
      }
      save_permissions(obj).then(res => {
        setTimeout(() => {
          this.fetchData()
          this.$message({
            message: '提交成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.loading = false
        }, 300)
      })
    }
  }
}
</script>
